//
//  Seach_FittrateSeting_VC_WX.m
//  ZITHER
//
//  Created by weixin on 2019/3/19.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "Seach_FittrateSeting_VC_WX.h"
#import "OKBtnScroll.h"
#import "Seach_FiltrateSetingList_Collection_Moudle_WX.h"
#import "Seach_FiltrateBottomDef_View_WX.h"


#import "Seach_FiltrateBrand_CollectionViewCell_WX.h"
#import "Seach_FiltrateAttribute_CollectionViewCell_WX.h"
#import "Seach_SearchResult_VC_WX.h"
#import "Seach_SearchResultMain_View_WX.h"


@interface Seach_FittrateSeting_VC_WX ()

@property (nonatomic,strong)Seach_FiltrateSetingList_Collection_Moudle_WX *WxCollectionV;

@property (nonatomic,strong)OKBtnScroll * BtnScroll;

@property (nonatomic,strong)Seach_FiltrateBottomDef_View_WX * BottomV;//底部通用view


@property (nonatomic,strong)Seach_SearchResultMain_View_WX * mainView;
@end

@implementation Seach_FittrateSeting_VC_WX

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNav];
    [self loadData];
}

-(void)setNav{
    [self.baseNavigationView NavigationBarStyle:1];
    self.baseNavigationView.leftButton1.hidden = YES;
    self.baseNavigationView.navViewStyle.title.text = @"筛选";
    [self.baseNavigationView setHiddenNav:NO VC:self];
}

-(void)setUITset{
    [self.view addSubview:self.mainView];
}


/* MARK: ================= custom =================*/
-(Seach_SearchResultMain_View_WX *)mainView{
    if (!_mainView) {
        _mainView = kUSER_SearchResultMain_MANAGER;
    }
    _mainView.indexType = 0;
    return _mainView;
}

-(void)loadData{
    
    //    if (kUSER_Filtrate_MANAGER.arrAttribute.count!=0) {
    //        [self initDataDone];
    //        return;
    //    }
    
    [[serviceDefualt loadSearchAttribute:nil]subscribeNext:^(ResponseData* x) {
        if (x.success) {
            [kUSER_Filtrate_MANAGER resetData];
            
            NSArray *arrData = x.responseRawData[@"data"];
            NSMutableArray *arrM = [[NSMutableArray alloc]init];
            for (NSDictionary *dic in arrData) {
                Seach_FiltrateAttribute_Model_WX *model = [Seach_FiltrateAttribute_Model_WX yy_modelWithDictionary:dic];
                if ([model.name isEqualToString:@"品牌"]) {
                    [arrM insertObject:model atIndex:0];
                }else if ([model.name isEqualToString:@"价格"]){
                    Seach_FiltrateAttributeSub_Model_WX *modelChild = model.child[1];
                    for (Seach_FiltrateAttributeSubData_Model_WX *modelData in modelChild.data) {
                        modelData.ispublicPrice = YES;
                    }
                    [arrM addObject:model];
                }else{
                    [arrM addObject:model];
                }
            }
            kUSER_Filtrate_MANAGER.arrAttribute = arrM;
            [self initDataDone];
        }else{
            [Tools showTips:x.errorInfo];
        }
        
    }];
}

-(void)initDataDone{
    [self setBrand];
    [self setTitle];
    [self setModel];
    
//    [self setUITset];

    [self setUI];
}

-(void)setModel{
    for (Seach_FiltrateAttribute_Model_WX *modelarrAttribute in kUSER_Filtrate_MANAGER.arrAttribute) {
        for (Seach_FiltrateAttributeSub_Model_WX *modelchild in modelarrAttribute.child) {
            for (Seach_FiltrateAttributeSubData_Model_WX *modelData in modelchild.data) {
                modelData.groupId = modelchild.groupId;
            }
        }
    }
}

//重组数据结构
-(void)setBrand{
    NSMutableArray *arrBigWord = [[NSMutableArray alloc]init];
    for (Seach_FiltrateAttributeSub_Model_WX *model in kUSER_Filtrate_MANAGER.arrAttribute[0].child) {
        if ([model.groupName isEqualToString:@"HOT"]) {
            [arrBigWord addObject:@"热"];
        }else{
            [arrBigWord addObject:model.groupName];
        }
        
    }
    kUSER_Filtrate_MANAGER.arrBrandBigWord = arrBigWord;
}

-(void)setTitle{
    
    NSMutableArray *arrTtitle = [[NSMutableArray alloc]init];
    
    for (Seach_FiltrateAttribute_Model_WX *model in kUSER_Filtrate_MANAGER.arrAttribute) {
        if ([model.name isEqualToString:@"品牌"]) {
            [arrTtitle insertObject:model.name atIndex:0];
        }else if([model.name isEqualToString:@"价格"]) {
            [arrTtitle insertObject:model.name atIndex:1];
        }else{
            [arrTtitle addObject:model.name];
        }
    }
    
    self.BtnScroll.arrM = arrTtitle;
    self.WxCollectionV.arrTitle = arrTtitle;
}

-(void)setUI{
    [self.view addSubview:self.BtnScroll];
    [self.view addSubview:self.WxCollectionV];
    [self.view addSubview:self.BottomV];
    
    //    _BtnScroll.arrM = [[NSMutableArray alloc]initWithArray:@[@"品牌",@"价格",@"基础参数",@"外观",@"功能",@"功能1",@"功能2",@"功能3"]];
    //    _WxCollectionV.arrTitle = [[NSMutableArray alloc]initWithArray:@[@"品牌",@"价格",@"基础参数",@"外观",@"功能",@"功能1",@"功能2",@"功能3"]];
    
    [self.BottomV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.WxCollectionV.mas_bottom).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(-kBottomSafeHeight);
        
        
        //        make.height.mas_equalTo(100+kBottomSafeHeight);
    }];
    
}

-(OKBtnScroll*)BtnScroll
{
    if (_BtnScroll) {
        return _BtnScroll;
    }
    _BtnScroll = [[OKBtnScroll alloc]initWithFrame:CGRectMake(0, NaviHeight,kScreenW, 40)];
    @weakify(self);
    [_BtnScroll setBlock:^(NSUInteger index) {
        @strongify(self);
        //        self.WxCollectionV.contentOffset = CGPointMake(kScreenW*(index-100), 0);
        //        [self refdata:index-100];
        
        [self.WxCollectionV setContentOffset:CGPointMake(kScreenW*(index-100), 0) animated:YES];
        
    }];
    //        _BtnScroll.arrM = self.arrBtn;
    return _BtnScroll;
}


-(Seach_FiltrateSetingList_Collection_Moudle_WX *)WxCollectionV
{
    if (_WxCollectionV) {
        return _WxCollectionV;
    }
    
    
    //kScreenH-NaviHeight-40-100-kBottomSafeHeight
    CGFloat h = kScreenH-NaviHeight-40-100-kBottomSafeHeight;
    
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    
    layout.minimumInteritemSpacing=0;
    
    layout.minimumLineSpacing = 0;
    
    layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
    
    //    if (self.State) {
    //        statements
    //    }
    //kScreenH-40-NaviHeight-100-kBottomSafeHeight
    layout.itemSize = CGSizeMake(SCREEN_WIDTH,h);
    
    _WxCollectionV = [[Seach_FiltrateSetingList_Collection_Moudle_WX alloc]initWithFrame:CGRectMake(0, NaviHeight+40, SCREEN_WIDTH, h) collectionViewLayout:layout];
    
    @weakify(self);
    [_WxCollectionV setBlock:^(NSInteger index) {
        @strongify(self);
        NSLog(@"%ld.index ",index);
        [self.BtnScroll changeBtn:index+100];
        [self refdata:index];
    }];
    [RACObserve(self.WxCollectionV,contentOffset)subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        int newContent = self.WxCollectionV.contentOffset.x;
        int width = kScreenW;
        if (newContent%width == 0) {
            [self refdata:newContent/width];
        }
    }];
    return _WxCollectionV;
}

- (Seach_FiltrateBottomDef_View_WX *)BottomV{
    if (!_BottomV) {
        _BottomV = [[Seach_FiltrateBottomDef_View_WX alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 0)];
        @weakify(self);
        
        [_BottomV setBlock:^(NSInteger index1) {
            @strongify(self);
            [self refdata:index1];
        }];
        
        
        [[self.BottomV.resetBtn addTag]subscribeNext:^(id  _Nullable x) {
            
            NSArray *arrSelect = kUSER_Filtrate_MANAGER.arrBrandSelect;
            
            for (int i =0; i<arrSelect.count; i++) {
                Seach_FiltrateAttributeSubData_Model_WX *model = arrSelect[i];
                model.isSelect = NO;
                NSArray *arr =  kUSER_Filtrate_MANAGER.arrAttribute[0].child[model.indexPath.section].data;
                for (int i = 0; i< arr.count; i++) {
                    Seach_FiltrateAttributeSubData_Model_WX *model3 = arr[i];
                    if ([model.labelValue[0] isEqualToString:model3.labelValue[0]]) {
                        model3 = model;
                        break;
                    }
                }
            }
            [kUSER_Filtrate_MANAGER.arrBrandSelect removeAllObjects];
            [self.BottomV addScrImage:kUSER_Filtrate_MANAGER.arrBrandSelect];
            [self.BottomV refload];
            if (self.BottomV.block) {
                [self.BtnScroll changeBtn:100];
                self.BottomV.block(999);
            }
        }];
        
        [[self.BottomV.doneBtn addTag]subscribeNext:^(id  _Nullable x) {
            if (kUSER_Filtrate_MANAGER.arrAttribute.count>0) {
                Seach_SearchResult_VC_WX *vc = [[Seach_SearchResult_VC_WX alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                [Tools showTips:@"数据错误，无法跳转"];
            }
            //            Seach_SearchResult_VC_WX *vc = [[Seach_SearchResult_VC_WX alloc]init];
            //            [self.navigationController pushViewController:vc animated:YES];
        }];
        
        
    }
    return _BottomV;
}


-(void)refdata:(NSInteger)index1{
    
    if (index1==0) {
        NSIndexPath * index = [NSIndexPath indexPathForRow:index1 inSection:0];
        Seach_FiltrateBrand_CollectionViewCell_WX *cell = (Seach_FiltrateBrand_CollectionViewCell_WX*)[self.WxCollectionV cellForItemAtIndexPath:index];
        [cell.TB reloadData];
        [cell.TB beginUpdates];
        [cell.TB endUpdates];
    }else if (index1 == 1) {
        NSIndexPath * index = [NSIndexPath indexPathForRow:index1 inSection:0];
        Seach_FiltrateAttribute_CollectionViewCell_WX *cell = (Seach_FiltrateAttribute_CollectionViewCell_WX*)[self.WxCollectionV cellForItemAtIndexPath:index];
        [cell.TB reloadData];
        [cell.TB beginUpdates];
        [cell.TB endUpdates];
    }else if (index1 == 999){
        NSIndexPath * index = [NSIndexPath indexPathForRow:0 inSection:0];
        Seach_FiltrateBrand_CollectionViewCell_WX *cell = (Seach_FiltrateBrand_CollectionViewCell_WX*)[self.WxCollectionV cellForItemAtIndexPath:index];
        [cell.TB reloadData];
        [cell.TB beginUpdates];
        [cell.TB endUpdates];
        [self.WxCollectionV setContentOffset:CGPointMake(0, 0) animated:YES];
        //        self.WxCollectionV.contentOffset = CGPointMake(0, 0);
    }else{
        NSIndexPath * index = [NSIndexPath indexPathForRow:index1 inSection:0];
        Seach_FiltrateAttribute_CollectionViewCell_WX *cell = (Seach_FiltrateAttribute_CollectionViewCell_WX*)[self.WxCollectionV cellForItemAtIndexPath:index];
        [cell.TB reloadData];
        [cell.TB beginUpdates];
        [cell.TB endUpdates];
    }
    
    
}
@end


/*
 
 */
